//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var manual_id=thisform.find('input[name=manual_id]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var cloud_depot_status=thisform.find('.cloud_depot_status').combobox('getValue');
		var grounding_status=thisform.find('.grounding_status').combobox('getValue');
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		var time_cate=thisform.find('input[name=time_cate]').val();
		var search_storage_code=thisform.find('input[name=search_storage_code]').val();
		// console.log(inbound_status);
		// return false;
		$('#groundingDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#groundingDatagrid').datagrid({ 
			queryParams:{
				status:1,//已审核通过
				grounding_status:grounding_status,//未上架
				order_no:order_no,
				manual_id:manual_id,
				goods_no:goods_no,
				goods_name:goods_name,
				inbound_status:1,//已入库
				cloud_depot_status:cloud_depot_status,
				start_time:start_time,
				end_time:end_time,
				time_cate:time_cate,
				search_storage_code:search_storage_code
			}
		});
	})
    //导出Excel
    $('.excelOut').click(function(){
        var thisform=$(this).parent('div').find('form');
        var order_no=thisform.find('input[name=order_no]').val();
        var manual_id=thisform.find('input[name=manual_id]').val();
        var goods_no=thisform.find('input[name=goods_no]').val();
        var goods_name=thisform.find('input[name=goods_name]').val();
        var cloud_depot_status=thisform.find('.cloud_depot_status').combobox('getValue');
        var grounding_status=thisform.find('.grounding_status').combobox('getValue');
        var start_time=thisform.find('input[name=start_time]').val();
        var end_time=thisform.find('input[name=end_time]').val();
        var time_cate=thisform.find('input[name=time_cate]').val();
        var search_storage_code=thisform.find('input[name=search_storage_code]').val();

        getarrivalUrl=getarrivalUrl+"&excel=1&inbound_status=1&status=1";
	
        if(order_no!=""){
            getarrivalUrl=getarrivalUrl+"&order_no="+order_no;
        }
		if(manual_id!=""){
            getarrivalUrl=getarrivalUrl+"&manual_id="+manual_id;
        }
        if(goods_no!=""){
            getarrivalUrl=getarrivalUrl+"&goods_no="+goods_no;
        }
		if(goods_name!=""){
            getarrivalUrl=getarrivalUrl+"&goods_name="+goods_name;
        }
		if(cloud_depot_status!=""){
            getarrivalUrl=getarrivalUrl+"&cloud_depot_status="+cloud_depot_status;
        }
		if(grounding_status!=""){
            getarrivalUrl=getarrivalUrl+"&grounding_status="+grounding_status;
        }
		if(time_cate!=""){
            getarrivalUrl=getarrivalUrl+"&time_cate="+time_cate;
        }
		if(search_storage_code!=""){
            getarrivalUrl=getarrivalUrl+"&goods_no="+search_storage_code;
        }
        if(start_time!=""){
            getarrivalUrl=getarrivalUrl+"&start_time="+start_time;
        }
        if(end_time!=""){
            getarrivalUrl=getarrivalUrl+"&end_time="+end_time;
        }

        window.location.href=getarrivalUrl;
    })
	//更新行数据
	function updateActions(value){
		$.post(confirmGroundingUrl,{id:value.id,storage_code:value.storage_code},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#groundingDatagrid').datagrid('validateRow', editIndex)){
			$('#groundingDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//单击行
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#groundingDatagrid').datagrid('selectRow', index);
				$('#groundingDatagrid').datagrid('endEdit', editIndex);
				$('#groundingDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#groundingDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	
	$('#groundingDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getarrivalUrl,
		title:'商品上架',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		queryParams:{
			status:1,
			inbound_status: 1,
			grounding_status:0//未上架
		},
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'manual_id',title:'账册编号',sortable:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称',sortable:true},
		{field:'goods_spec',title:'规格'},
		{field:'price',title:'单价',sortable:true},
		{field:'receive_amount',title:'实际到货数量',sortable:true},
		{field:'storage_amount',title:'库存数量',sortable:true},
		{field:'freez_amount',title:'冻结数量',sortable:true},
		{field:'bar_code',title:'条形码',
			editor: {
                type: 'numberbox',  
                options: { 
                    required: true,  
                    missingMessage: '不能为空',
					min:1
                }
            }
		},
		{field:'storage_code',title:'库位',sortable:true,width:'100px',
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'name',
					method:'get',
					url:storageListUrl,
					required:true,
					validType:['selected[]']
				}
			}
		},
		{field:'area',title:'面积',sortable:true},
		{field:'overdue_time',title:'过期时间',sortable:true,width:150,
			editor:{
				type:'datetimebox'
			}
		},
		{field:'overdue_time_str',title:'过期剩余天数',sortable:true,width:150},
		{field:'cloud_depot_status',title:'是否云仓商品',sortable:true},
		{field:'cloud_num',title:'云仓商品数量',sortable:true},
		{field:'cloud_unit',title:'云仓商品单位',sortable:true},
		{field:'conforming_p',title:'合格品',sortable:true},
		{field:'nonconforming_p',title:'残次品',sortable:true},
		{field:'scrap_one',title:'报废品',sortable:true},
		// {field:'push_grounding_time',title:'推送上架时间',sortable:true},
		// {field:'push_grounding_times',title:'推送上架次数',sortable:true},
		// {field:'expected_time',title:'预计到货时间',sortable:true},
		{field:'arrival_time',title:'到货时间',sortable:true},
		// {field:'push_arrival_time',title:'推送实收时间',sortable:true},
		// {field:'examine_time',title:'审核时间',sortable:true}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		pagination:true,
		fit:true,
	});
});
groundingObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#groundingDatagrid').datagrid('validateRow', editIndex)){
			$('#groundingDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#groundingDatagrid').datagrid('acceptChanges');
			$('#groundingDatagrid').datagrid('reload');
		}
	},
	ungroundingBar:function(){
		var groundingRow=$('#groundingDatagrid').datagrid('getSelections');
        if (groundingRow.length>0){
        	$.messager.confirm('下架提示','真的要下架这'+groundingRow.length+'个商品吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<groundingRow.length;i++){
                		ids.push(groundingRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(ungroundingUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#ungroundingAddDialog').dialog('close');       
								$('#groundingDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	groundingBar:function(){
		var groundingRow=$('#groundingDatagrid').datagrid('getSelections');
        if (groundingRow.length>1){		            	
            $.messager.alert('提示','一次只能上架一条记录!','info');
        }else if(groundingRow.length==1){            	
        	$('#groundingEditDialog').dialog('open').dialog('setTitle','商品上架');
            $('#groundingEditForm').form('load',groundingRow[0]);
			$('#groundingGroupEdit').combobox('select',0);
			$.ajax({
				type:'POST',
				data:{id:groundingRow[0]['id']},
				url: get_storagehistoryinfo,
				success:function(data){
					if(data.status==1){	//检查通过，跳转生成波次单页面
						thisselect=$('#_easyui_combobox_i4_0').parent();
						children=thisselect.find('div');
						for(i=0;i<children.length;i++){
							if(children.eq(i).text()==data.message.name){
								$('#groundingGroupEdit').combobox('setValue',data.message.storage_code);
								console.log(data.message.name);
							}
						}
					}else{				//检查未通过，刷新页面
						// console.log('没有历史库位');
						thisselect=$('#_easyui_combobox_i4_0').parent();
						children=thisselect.find('div');
						// console.log(children.eq(1).text());
					}
					return false;
				}
			})
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	save:function(url){
		$('#groundingEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#groundingEditDialog').dialog('close');
                    });
                }else{
                    $('#groundingEditDialog').dialog('close');      
                    $('#groundingDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#groundingDatagrid').datagrid({ queryParams:{status:1,grounding_status:0,inbound_status:1}});//已入库参数
	},
	reloadBar:function(){
		$('#groundingDatagrid').datagrid('reload');
	},
	pushBar:function(){
		var groundingRow=$('#groundingDatagrid').datagrid('getSelections');
		var text='确定推送吗！';
        if (groundingRow.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<groundingRow.length;i++){
                		ids.push(groundingRow[i].id);
						// if(groundingRow[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+groundingRow[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(groundingpushUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}

}